﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Assignment_2
{
    class TheWorldMap
    {
        #region Variables
        private WorldMapStuff wms;
        private VertexPositionColor[] vertices;
        private short[] indices;
        private System.Drawing.Color c;
        private int totalNumberOfPixels;
        private int indicesPosition = 0;
        private int verticesPosition = 0;

        public VertexBuffer mapVB;

        public IndexBuffer mapIB;

        GraphicsDevice graphicsDevice;
        #endregion

        #region Constants
        private const int FTR = 0;
        private const int FTL = 1;
        private const int FBR = 2;
        private const int FBL = 3;
        // Is not used
        private const float maxZ = 1f;
        #endregion

        public TheWorldMap(GraphicsDevice graphicsDevice)
        {
            this.graphicsDevice = graphicsDevice;

            InitPixelRectangles();            

            mapVB = new VertexBuffer(graphicsDevice, typeof(VertexPositionColor), vertices.Length, BufferUsage.WriteOnly);
            mapIB = new IndexBuffer(graphicsDevice, typeof(short), indices.Length, BufferUsage.WriteOnly);

            mapVB.SetData<VertexPositionColor>(vertices);
            mapIB.SetData<short>(indices);
        }

        public void Draw()
        {
            graphicsDevice.SetVertexBuffer(mapVB);
            graphicsDevice.Indices = mapIB;

            graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertices.Length, 0, indices.Length / 3);
        }

        private void InitVertices()
        {
            vertices = new VertexPositionColor[totalNumberOfPixels * 4];
        }

        private void InitIndices()
        {
            indices = new short[totalNumberOfPixels * 6];
        }

        private void InitPixelRectangles()
        {
            float x = 1;
            float y = 1;
            wms = new WorldMapStuff();
            PixelRectangleTopLeft prtl;
            PixelRectangleTopRight prtr;
            PixelRectangleBottomRight prbr;
            PixelRectangleBottomLeft prbl;

            totalNumberOfPixels = wms.height * wms.width;
            InitVertices();
            InitIndices();
            
            // We're building top left, down then jumping two steps to the right and doing the same thing.
            // i += 2 because we're building a square sized 2*2
            for (int i = 0; i < wms.width; i+=2)
            {
                // j += 2 because we're building a square sized 2*2
                for (int j = 0; j < wms.height; j+=2)
                {
                //felet är här indices?
                    c = wms.img.GetPixel(i, j);
                    prtl = new PixelRectangleTopLeft(new Microsoft.Xna.Framework.Color(c.R, c.G, c.B, c.A), x, y);
                    x++;
                    c = wms.img.GetPixel(i+1, j);
                    prtr = new PixelRectangleTopRight(new Microsoft.Xna.Framework.Color(c.R, c.G, c.B, c.A), x, y);
                    y++;
                    c = wms.img.GetPixel(i, j+1);
                    prbr = new PixelRectangleBottomRight(new Microsoft.Xna.Framework.Color(c.R, c.G, c.B, c.A), x, y);
                    x++;
                    c = wms.img.GetPixel(i, j+1);
                    prbl = new PixelRectangleBottomLeft(new Microsoft.Xna.Framework.Color(c.R, c.G, c.B, c.A), x, y);
                    y++;

                    AddIndices(prtl, prtr, prbr, prbl);
                    AddVertices(prtl, prtr, prbr, prbl);
                    //prs[i, j] = new PixelRectangle(GraphicsDevice, new Microsoft.Xna.Framework.Color(c.R, c.G, c.B, c.A));

                }
                y = 1;
                x = i;
            }
        }

        private void AddIndices(PixelRectangleTopLeft prtl,  PixelRectangleTopRight prtr, PixelRectangleBottomRight prbr, PixelRectangleBottomLeft prbl)
        {
            for (int k = 0; k < 6; k++)
            {
                indices[indicesPosition + k] = prtl.indices[k];                
            }
            indicesPosition += 6;
            for (int k = 0; k < 6; k++)
            {
                indices[indicesPosition + k] = prtr.indices[k];
            }
            indicesPosition += 6;
            for (int k = 0; k < 6; k++)
            {
                indices[indicesPosition + k] = prbr.indices[k];
            }
            indicesPosition += 6;
            for (int k = 0; k < 6; k++)
            {
                indices[indicesPosition + k] = prbl.indices[k];
            }
            indicesPosition += 6;            
        }

        private void AddVertices(PixelRectangleTopLeft prtl, PixelRectangleTopRight prtr, PixelRectangleBottomRight prbr, PixelRectangleBottomLeft prbl)
        {
            for (int k = 0; k < 4; k++)
            {
                vertices[verticesPosition + k] = prtl.vertices[k];
            }       
            verticesPosition += 4;
            for (int k = 0; k < 4; k++)
            {
                vertices[verticesPosition + k] = prtr.vertices[k];
            }       
            verticesPosition += 4;
            for (int k = 0; k < 4; k++)
            {
                vertices[verticesPosition + k] = prbr.vertices[k];
            }       
            verticesPosition += 4;
            for (int k = 0; k < 4; k++)
            {
                vertices[verticesPosition + k] = prbl.vertices[k];
            }       
            verticesPosition += 4;            
        }
    }
}
